package com.welbell.utlis;

import android.graphics.Bitmap;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

public class Base64Utlis {


    /**
     * 文件转base64字符串
     *
     * @param file
     * @return
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        return base64;
    }


    public static String bitmaptoString(Bitmap bitmap) {
        // 将Bitmap转换成字符串
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bStream);
        byte[] bytes = bStream.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    /**
     * 存文件
     * @param content
     */
    public static void save(String content,String path) {
        try {
            File file = new File(path);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
                file.createNewFile();
            }
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = dateformat.format(System.currentTimeMillis());

            //新建一个FileOutputStream()，把文件的路径传进去
            FileOutputStream fileOutputStream = new FileOutputStream(file, true);
            //通过输出流对象写入字节数组
            fileOutputStream.write((content).getBytes());
            //关流
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * base64字符串转文件
     *
     * @param base64 字符串
     * @param file   完整文件路径
     * @return
     */
    public static File base64ToFile(String base64, File file) {
        FileOutputStream out = null;
        try {
            // 解码，然后将字节转换为文件
            if (!file.exists())
                file.createNewFile();
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);// 将字符串转换为byte数组
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(file);
            int bytesum = 0;
            int byteread = 0;
            while ((byteread = in.read(buffer)) != -1) {
                bytesum += byteread;
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        return file;
    }


    /**
     * AES加密
     *
     * @param data
     * @return
     */
//    public static String encrypt(String data, String key) {
//        try {
//            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
//            SecretKeySpec skeySpec = new SecretKeySpec(new Base64.decodeBuffer(key), "AES");
//            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
//            byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
//            return new Base64.encode(encrypted);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    /**
//     * AES解密
//     */
//    public static String decrypt(String data, String key) {
//        try {
//            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//            SecretKeySpec skeySpec = new SecretKeySpec(new Base64.decodeBuffer(key), "AES");
//            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//            byte[] encrypted = cipher.doFinal(new Base64.decodeBuffer(data));
//            return new String(encrypted, "UTF-8");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

}
